//
//  UIButton+Extension.swift
//  01- 搭建框架
//
//  Created by as888 on 15/10/16.
//  Copyright © 2015年 任伟. All rights reserved.
//

import UIKit

extension UIButton {
    
    /// `便利`构造函数
    convenience init(imageName: String, backImageName: String?) {
        self.init()
        
        setImage(UIImage(named: imageName), forState: UIControlState.Normal)
        setImage(UIImage(named: imageName + "_highlighted"), forState: UIControlState.Highlighted)
        if let backImageName = backImageName {
            setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
            setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        }
        
        // 会根据背景图片的大小调整尺寸
        sizeToFit()
    }
    
    
    ///遍历构造函数
    convenience init(title: String,
        //fontSize: CGFloat,
        color: UIColor,
        imageName: String) {
        self.init()
        
        setTitle(title, forState: UIControlState.Normal)
        setTitleColor(color, forState: UIControlState.Normal)
        setBackgroundImage(UIImage(named: imageName), forState: UIControlState.Normal)
        
       // titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        
        //sizeToFit()
    }
    
    ///遍历构造函数
    convenience init(title: String,
        fontSize: CGFloat,
        color: UIColor,
        imageName: String?, backColor: UIColor? = nil) {
            self.init()
            
            setTitle(title, forState: UIControlState.Normal)
            setTitleColor(color, forState: UIControlState.Normal)
            
            if let imageName = imageName {
            
                setImage(UIImage(named: imageName), forState: UIControlState.Normal)
            }
            
            titleLabel?.font = UIFont.systemFontOfSize(fontSize)
            
            backgroundColor = backColor
            
            sizeToFit()
    }

    
}
